/**
 * 载入项目引导文件
 */
require('./bootstrap');

window.Vue = require('vue');

Vue.component('WebuploaderSingle', require('../components/WebuploaderSingle.vue'));

let cat_empty = {
    'card_base_id': 0,
    'key' : '',
    'name' : '',
    'description':'',
    'background_pic_url':'',
    'give_bonus':0,
    'give_coupon':'',
    'order_num':0,
};

const app = new Vue({
    el: '#wrapper',
    data: {
        curLevelData:{},
        selectCatData:{},
        card_base_id:0,
        level_key_list:page_params.data_level_key_list,
        box:null,
        asxBox:null,
    },
    watch:{
        card_base_id:function () {
            this.initTree();
        }
    },
    mounted:function(){
        this.initTree();
    },
    methods:{
        initTree:function () {
            let _this = this;
            let tree_data = {};
            utils.ajaxGet(page_params.url_list, {'cardBaseId':this.card_base_id}, (response, data) =>{
                if (response.data.levelList){
                    tree_data = response.data.levelList;
                    $('#tree').treeview({data: tree_data,levels:2,showTags:!0,collapseIcon:'fa fa-chevron-down',expandIcon:'fa fa-chevron-right'});
                    //绑定选中事件
                    $('#tree').on('nodeSelected', (event, data) => {
                        this.asxBox = layer.open({
                            type: 1,
                            title: '会员等级操作',
                            anim: 2,
                            shadeClose: true, //开启遮罩关闭
                            skin: 'layui-layer-molv', //加上边框
                            area: ['520px', '150px'], //宽高
                            content: $('#box_ask')
                        });
                        _this.selectCatData = data;
                    });
                }
            });
        },
        add:function(){
            this.curLevelData = _.cloneDeep(cat_empty);
            this.curLevelData.card_base_id = this.card_base_id;
            this.box = layer.open({
                type: 1,
                title: '添加会员等级',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '450px'], //宽高
                content: $('#box-form')
            });
        },
        edit : function (){
            this.curLevelData = _.cloneDeep(this.selectCatData);
            this.box = layer.open({
                type: 1,
                title: '编辑【' + this.selectCatData.name + '】会员等级',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['620px', '450px'], //宽高
                content: $('#box-form')
            });
        },
        save:function () {
            let _this = this;
            let saveUrl = _.isEmpty(this.curLevelData.id) ? page_params.url_add : page_params.url_edit;
            utils.ajaxPost(saveUrl, this.curLevelData, (response, postdata) =>{
                let data = response.data;
                utils.showToastr(data);
                if (data.status == 'success'){
                    layer.close(this.box);
                    layer.close(this.asxBox);
                    _this.initTree();
                }
            });
        },
        del:function () {
            let _this = this;
            utils.confirm(page_params.url_del, {'id': this.selectCatData.id}, function (response, postdata) {
                data = response.data;
                utils.showToastr(data);
                if (data.status == 'success'){
                    layer.close(this.asxBox);
                    _this.initTree();
                }
            });
        }
    },
});